package com.beijiteshop.shop.ui.home.vm

import androidx.lifecycle.MutableLiveData
import com.beijiteshop.libcommon.base.viewmodel.BaseViewModel
import com.beijiteshop.libcommon.network.ext.onBizError
import com.beijiteshop.libcommon.network.ext.onBizOK
import com.beijiteshop.libcommon.network.ext.serverData
import com.beijiteshop.libcommon.network.model.onFailure
import com.beijiteshop.libcommon.network.model.onSuccess
import com.beijiteshop.shop.model.entity.ProductItemEntity
import com.beijiteshop.shop.ui.home.repo.HomeRepo
import kotlinx.coroutines.async

/**
 * @Author LiaoZhongKai
 * @Date 2021/3/1 16:45
 * @Description 首页ViewModel
 */
class HomeVM(private val repo: HomeRepo) : BaseViewModel() {

    val liveHomeData = MutableLiveData<MutableList<ProductItemEntity>>()

    /**
     * 获取首页抢购商品数据和推荐商品数据
     */
    fun getHomeGoods(page: Int) = launch {
        val result = mutableListOf<ProductItemEntity>()
        val discountGoods = async { repo.getDiscountGoods(page).serverData()}
        val productList = async { repo.getProductList(page).serverData() }
        val goods = discountGoods.await()
        val products = productList.await()
        goods.onSuccess {
            onBizOK { code, data, message ->
                if (!data?.data.isNullOrEmpty()){
                    result.add(
                        ProductItemEntity(
                            ProductItemEntity.TYPE_HEADER_RUSH,
                            2,
                            null
                        )
                    )
                    data!!.data!!.forEach {
                        result.add(
                            ProductItemEntity(
                                ProductItemEntity.TYPE_RUSH_GOODS,
                                2,
                                it
                            )
                        )
                    }
                }
            }
        }

        products.onSuccess {
            onBizOK { code, data, message ->
                if (data?.data.isNullOrEmpty()){
                    liveEmpty.value = message
                }else{
                    result.add(
                        ProductItemEntity(
                            ProductItemEntity.TYPE_HEADER_REC,
                            2,
                            null
                        )
                    )
                    data!!.data!!.forEach {
                        result.add(
                            ProductItemEntity(
                                ProductItemEntity.TYPE_COMMON_GOODS,
                                1,
                                it
                            )
                        )
                    }
                    liveHomeData.value = result
                }
            }

            onBizError { code, message ->
                liveError.value = message
            }
        }.onFailure {
            liveNetError.value = it.message
        }
    }


    /**
     * 获取推荐商品数据
     */
    fun loadMoreProducts(page: Int) = launch {
        val result = mutableListOf<ProductItemEntity>()
        repo.getProductList(page)
            .serverData()
            .onSuccess {
                onBizOK { code, data, message ->
                    if (data?.data.isNullOrEmpty()){
                        liveEmpty.value = message
                    }else{
                        data!!.data!!.forEach {
                            result.add(
                                ProductItemEntity(
                                    ProductItemEntity.TYPE_COMMON_GOODS,
                                    1,
                                    it
                                )
                            )
                        }
                        liveHomeData.value = result
                    }
                }

                onBizError { code, message ->
                    liveError.value = message
                }
            }
            .onFailure {
                liveNetError.value = it.message
            }
    }


}